package isssr.dao.test;

import org.junit.Test;

import isssr.dao.exception.DAOException;
import isssr.dao.factory.DAOFactory;
import isssr.entity.src.*;

public class HibernateStoragePopulator 
{
	private static int sequenceNumber = 0;
	
	@Test
	public void populate() throws DAOException
	{
		try
		{
			Integer level;
			Role role;
			User user = null;
			
			Integer numberOfRoles = 5;
			Integer numberOfUserPerRole = 3;
			
			for(int i=0; i<numberOfRoles; i++)
			{
				level = 10 * i;
				role = createAndPersistRole(level);
				
				for(int j=0; j<numberOfUserPerRole; j++)
				{
					user = createAndPersistUser(role);
				}
				
				if(numberOfUserPerRole>0 && user!=null)
					createAndPersistGoal(user);
			}
		}
		catch(DAOException ex)
		{
			ex.printStackTrace();
		}
	}
	
	private Integer getNextSequenceNumber()
	{
		return this.sequenceNumber++;
	}
	
	private Role createAndPersistRole(Integer level) throws DAOException
	{
		Role role = new Role("ROLE_" + getNextSequenceNumber(), level);
		
		DAOFactory.getDAOFactory().getRoleDAO().insert(role);
		return role;
	}
	
	private User createAndPersistUser(Role role) throws DAOException
	{
		User user = new User("USERNAME_" + getNextSequenceNumber(), role);
		user.setFirstPassword("pass");
		
		DAOFactory.getDAOFactory().getUserDAO().insert(user);
		return user;
	}
	
	private OrganizationalGoal createAndPersistGoal(User owner) throws DAOException
	{
		OrganizationalGoal goal = new OrganizationalGoal("GOAL_" + getNextSequenceNumber(), owner);
		
		DAOFactory.getDAOFactory().getOrganizationalGoalDAO().insert(goal);
		return goal;
		
	}
}
